<?php

namespace app\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Works;
use common\models\UserInterest;

/**
 * Default controller for the `api` module
 */
class UserController extends Controller
{
    public $enableCsrfValidation = false;
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $output = [];
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录，请先登录！";
            Tools::outputjson($output);
            exit;
        }
        $output['success'] = 1;
        $output['msg'] = "请求成功！";
        $userid = Yii::$app->user->identity->id;
        
        $data['username'] = Yii::$app->user->identity->username;
        $data['headimg'] = Yii::$app->user->identity->headimg;
        $data['worksnum'] = Works::getUserWorksNum($userid);
        $data['collectnum'] = 5;
        
        $output['data'] = $data;//Works::getUserWorks($userid);
        Tools::outputjson($output);
    }
    
    
    public function actionGetmobilecode(){
        $mobile = Tools::GetSafeParam('mobile', '', 1);
        
        if(!Tools::is_moblie($mobile)){
            $data['success'] = 0;
            $data['msg'] = "请输入手机号！";
            Tools::outputjson($data);
        }
        
        $code = mt_rand(1000, 10000);
        $code_str = "xingelvsms_".$code;
        $encrypt_code = Tools::encrypt($code_str, Yii::$app->params['sms_encrypt_key']);
        $data['hashcode'] = $encrypt_code;
        $data['send_back'] = \common\models\Sms::sendAuthCode($mobile, $code);
        Tools::outputjson($data);
    }
    
    public function actionInterest(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录，请先登录！";
            Tools::outputjson($output);
        }
        $authorid = Tools::GetSafeParam('authorid', 0, 0);
        $userid = Yii::$app->user->identity->id;
        $is_interested = UserInterest::checkIsInterested($userid, $authorid);
        if(!$is_interested){
            $model = new UserInterest();
            $model->userid = $userid;
            $model->interest_id = $authorid;
            $model->addtime = time();
            if($model->save()){
                $output['success'] = 1;
                $output['msg'] = "关注成功！";
                Tools::outputjson($output);
            }else{
                $output['success'] = 2;
                $output['msg'] = "系统错误！";
                Tools::outputjson($output);
            }
        }else{
            $output['success'] = 2;
            $output['msg'] = "已关注！";
            Tools::outputjson($output);
        }
    }
    
    public function actionInterestCancel(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录，请先登录！";
            Tools::outputjson($output);
        }
        $authorid = Tools::GetSafeParam('authorid', 0, 0);
        $model = UserInterest::findOne(['userid'=>Yii::$app->user->identity->id,'interest_id'=>$authorid]);
        if(empty($model)){
            $output['success'] = 2;
            $output['msg'] = "未关注！";
            Tools::outputjson($output);
        }
        $model->delete();
        $output['success'] = 1;
        $output['msg'] = "取消关注成功！";
        Tools::outputjson($output);
    }
}
